<?php

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        $this->logger = Marjory_Service::getLog();
        parent::init();
    }

    public function preDispatch()
    {
        $view = $this->initView();
        $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
        //$innerView = new Zend_View();
        //$innerView->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
        //$innerView->setScriptPath($view->getScriptPaths());        
        
        #Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
        Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
        Zend_Dojo::enableView($view);
        //Zend_Dojo::enableView($innerView);
        
        //$this->contentView = $innerView;
    }
        
	public function indexAction()
	{
        //$this->view->dataDir = dirname(__FILE__) . '/../../data';
	    //$this->contentView->dataDir = dirname(__FILE__) . '/../../data';
        //$this->view->content = $this->contentView->render('index/index_content.phtml');
        $this->view->catalogList = array('default' => 'Default');
        foreach (new DirectoryIterator(MARJORY_DATA_DIR) as $dir) {
            if ((string)$dir != 'default' && !$dir->isDot() && substr((string)$dir,0,1) != '.') {
                $this->view->catalogList[(string)$dir] = ucfirst((string)$dir);
            }
        }
        $this->view->currentCatalog = 'default';
	    $this->render();
	}
	
	public function addAction() {
	    
        echo <<<EOD
<html>
<head>
    <style type="text/css">
    body {
        font-family: tahoma, arial, helvetica;
        font-size: 1em;
    }
    h1 {
        font-size: 1.2em;
    }
    h2 {
        font-size: 1.1em;
    }
    #results {
        border: 1px solid darkgrey;
        background-color: #E0E0E0;
    }
    </style>
    <script src="http://yui.yahooapis.com/2.3.1/build/yahoo/yahoo-min.js"></script>
    <script src="http://yui.yahooapis.com/2.3.1/build/event/event-min.js"></script>
    <script src="http://yui.yahooapis.com/2.3.1/build/connection/connection-min.js"></script> 
    <script type="text/javascript">
    function responseSuccess(r) {
        alert('Woohoo!');
        document.getElementById('results').innerHTML = r.responseText;
    }
    function responseFailure(r) {
        alert("Doh! " + r.responseText);
        document.getElementById('results').innerHTML = r.responseText;
    }
    function add(url, catalog, content) {
        var sUrl = '/rest/update/'; 
        var postData = '<add catalog="' + catalog + '">';
        if (url != "") {
            postData += '<doc src="' + url + '">';
        } else if (content != "") {
            var now = new Date();
            var uri = now.getTime();
            postData += '<doc uri="' + uri + '">';
            postData += '<field name="uri">' + uri + "</field>";
            postData += '<field name="content">' + content + "</field>";
        } else {
            alert('You must provide either a URL or some text as the document content');
            return false;
        }
        
        postData += '</doc></add>';
        var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, { success: responseSuccess,
                                                                         failure: responseFailure
                                                                        }, postData);    
    }
    function addCatalog(title) {
    	var sUrl = '/rest/catalog/';
    	var postData = '<add catalog="' + title + '" />';
    	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, { success: responseSuccess,
    																	  failure: responseFailure
    																    }, postData);
    }
    </script>
</head>
<body>
    <h1>Marjory index addition:</h1>
    <form action="javascript:;" onsubmit="add(this.url.value, this.catalog.value, this.content.value)">
        <label for="url">URL to add:</label>
        <input type="text" id="url" name="url" value="" />
        <label for="catalog">Catalogue to add to:</label>
        <select id="catalog" name="catalog">
        	<option value="default" selected="selected">[Default catalogue]</option>
EOD;
        foreach (new DirectoryIterator(dirname(__FILE__) . '/../../data') as $dir) {
            if ($dir != 'default' && !$dir->isDot() && $dir != '.svn') {
                echo "<option value=\"$dir\">$dir</option>";
            }
        }
        echo <<<EOD
        </select>
        <br />
        <label for="content">Content (leave empty when entering a URL):</label><br />
        <textarea name="content" id="content"></textarea>
        <input type="submit" value="Submit" name="submit" />
    </form>
    <br /><br />
    <form action="javascript:;" onsubmit="addCatalog(this.title.value)">
    	<label for="title">Add new catalog:</label>
    	<input type="text" id="title" name="title" value="" />
    	<input type="submit" value="Submit" name="submit" />
    </form>
    <h2>Results:</h2>
    <div id="results"></div>
</body>
</html>

EOD;

        #$this->render();
	}
        
    public function __call($method, $params)
    {
        $this->_response->setHttpResponseCode(404);
        echo "<html><head><title>404: Not found</title></head><body><h1>Error 404</h1><p>The URL you entered was not found at this server.</p></body></html>";	    
    }
}
